package com.finalab.yueqiu.dao.user;

import com.finalab.yueqiu.dao.user.po.UserPO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserDAO {

    String TABLE_NAME = "e_user";
    String INSERT_FIELDS = "password, phone, salt";
    String SELECT_FIELDS = "id, password, phone, salt";

    @Insert({"insert into ", TABLE_NAME, "(",INSERT_FIELDS,")","values(#{password}, #{phone}, #{salt})"})
    void insert(UserPO user);

    @Select({"select", SELECT_FIELDS, " from ", TABLE_NAME, "where id = #{id}"})
    UserPO selectById(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, "where phone = #{phone}"})
    UserPO selectByPhone(@Param("phone") String phone);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

    @Select({"select id from ", TABLE_NAME, " where exists(select * from ", TABLE_NAME,
            " where phone = #{phone})"})
    List<Object> exists(@Param("phone")String phone);



}
